package view
{
	
	import com.test.ui.*;
	
	import laya.display.Stage;
	import laya.display.Text;
	import laya.maths.Rectangle;
	import laya.net.Loader;
	import laya.utils.Browser;
	import laya.utils.Handler;
	import laya.webgl.WebGL;

	public class Game
	{
		
		private var hero:Role;
		
		public function Game() {
			//初始化引擎
			Laya.init(480, 852,WebGL);
			
			//创建循环滚动的背景
			var bg: Background = new Background();
			//把背景添加到舞台上显示出来
			Laya.stage.addChild(bg);
			
			Laya.loader.load("res/atlas/war.json", Laya.Handler.create(this, this.onLoaded), null, Laya.Loader.ATLAS)
		}
		
		private function onLoaded():void{
			//创建一个主角（主战斗机）
			hero= new Role();
			hero.init("hero",0,3,0,30);
			hero.pos(200,700);
			hero.shootType = 1;
			//把主角添加到舞台上
			Laya.stage.addChild(hero);
			Laya.stage.on("mousemove",this,onMouseMove);
			createEnemy(10);
			Laya.timer.frameLoop(1,this,onLoop);
			//显示FPS
			Laya.Stat.show();
		}
		
		private function onLoop():void
		{
			
			if (this.hero.hp < 1) {
				Laya.timer.clear(this, this.onLoop);
			}
			//处理飞机运动，产生子弹，role越界
			for(var i:Number = Laya.stage.numChildren -1;i > -1;i--){
				var role :Role = Laya.stage.getChildAt(i);
				if(role){
					if(role.speed){//子弹和敌机飞行
						role.y += role.speed;
						if(role.y > 800 || (role.isBullet && role.y < 0) ){
							role.removeSelf();
							Laya.Pool.recover("role",role);
						}
					}
					if(role.shootType > 0){
						var time:Number = Laya.Browser.now();
						if(time > role.shootTime){
							role.shootTime = time + role.shootInterval;
							var bullet : Role = Laya.Pool.getItemByClass("role",Role);
							bullet.init("bullet1",role.camp,1,-5,1);
							bullet.isBullet = true;
							bullet.pos(role.x,role.y-role.hitRadius - 10);
							Laya.stage.addChild(bullet);
						}
					}
					
				}
			}
			for(var i:Number = Laya.stage.numChildren -1;i > -1 ; i--){
				var iRole :Role = Laya.stage.getChildAt(i);
				for(var j:Number = Laya.stage.numChildren -1;j > -1 ; j--){
					if(i==j || iRole.hp <=0 || !iRole.visible){
						continue;
					}
					var jRole :Role = Laya.stage.getChildAt(j);
					if(jRole.hp > 0 && iRole.camp != jRole.camp){
						if(isHit(iRole,jRole)){
							this.loseHp(iRole,1);	
							this.loseHp(jRole,1);
						}
					}
				}
			}
			
			
			if(Laya.timer.currFrame % 30 == 0){
				this.createEnemy(1);
			}
		}
		
		private function isHit(role1:Role ,role2:Role):Boolean{
			var hitRadios:Number = role1.hitRadius + role2.hitRadius;
			if(Math.abs(role1.x - role2.x) < hitRadios && Math.abs(role1.y - role2.y) < hitRadios ){
				return true;
			}
		}
		
		private function loseHp(role:Role,lostHp:Number){
			role.hp  -= lostHp;
			if(role.hp <=0){
				if(role.isBullet){
					role.visible = false;
				}else{
					role.playAction("down");
				}
//				role.removeSelf();
//				Laya.Pool.recover("role",role);
			}else{
				role.playAction("hit");
			}
		}
		
		
		private function onMouseMove():void
		{
			this.hero.pos(Laya.stage.mouseX,Laya.stage.mouseY);
		}
		
		private var hps:Array = [1,2,10];
		private var speeds:Array=[3,2,1];
		private var radius:Array = [15,30,70];
		
		private function createEnemy(num:Number):void{
			for(var i :Number=0;i<num;i++){
				var r:Number = Math.random();
				var type:Number = r<0.5?0:(r<0.8?1:2);
				var enemy:Role = Laya.Pool.getItemByClass("role", Role);
				enemy.init("enemy" + (type + 1),1,this.hps[type],this.speeds[type],this.radius[type]);
				enemy.pos(Math.random() * 400 + 40,Math.random()*200);
				Laya.stage.addChild(enemy);
			}
		}
		
	}
}